package com.RMP
{
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.text.*;
    import com.greensock.*;

    public class SearchShell extends Sprite
    {
        var thumbnail:RecentImage;
        var myXMLList:XMLList;
        var counter:int;
        var xmlLoader:URLLoader;
        var thumbsArray:Array;
        var nav:ProjPaginatedMenu;
        var path:String = "flash_support/search_results_xml.php?search=";
        var fadeSpeed:Number;
        var searchResults:Sprite;
        var currNavId:int;
        public var header:MovieClip;
        var myRoot:MovieClip;
        var searchTerms:String;
        var myXML:XML;
        var startIndexNum:int;

        public function SearchShell(param1:MovieClip, param2:Number) : void
        {
            path = "flash_support/search_results_xml.php?search=";
            myRoot = param1;
            fadeSpeed = param2;
            path = myRoot.path + path;
            header.myTitle.antiAliasType = AntiAliasType.ADVANCED;
            header.myTitle.gridFitType = GridFitType.PIXEL;
            header.myInput.antiAliasType = AntiAliasType.ADVANCED;
            header.myInput.gridFitType = GridFitType.PIXEL;
            header.myMessage.antiAliasType = AntiAliasType.ADVANCED;
            header.myMessage.gridFitType = GridFitType.PIXEL;
            header.mySubmit.antiAliasType = AntiAliasType.ADVANCED;
            header.mySubmit.gridFitType = GridFitType.PIXEL;
            header.submitBtn.buttonMode = true;
            header.submitBtn.useHandCursor = true;
            header.submitBtn.addEventListener(MouseEvent.CLICK, clickListener);
            header.submitBtn.addEventListener(MouseEvent.MOUSE_OVER, overListener);
            header.submitBtn.addEventListener(MouseEvent.MOUSE_OUT, outListener);
            header.myInput.addEventListener(FocusEvent.FOCUS_IN, focusInListener);
            header.myInput.addEventListener(FocusEvent.FOCUS_OUT, focusOutListener);
            myRoot.stage.addEventListener(KeyboardEvent.KEY_DOWN, enterListener);
            myRoot.stage.addEventListener(Event.RESIZE, onResizer);
            doResize();
            if (myRoot.searchTerms != "")
            {
                searchTerms = myRoot.searchTerms;
                header.myInput.text = searchTerms;
                startXML(searchTerms);
            }
            return;
        }

        function outListener(event:MouseEvent) : void
        {
            TweenMax.to(header.submitBG, fadeSpeed, {tint:myRoot.coldColor, alpha:1, overwrite:true});
            return;
        }

        function doResize() : void
        {
            var _loc_1:int = 0;
            header.x = Math.round(4 * (myRoot.unitW + myRoot.gutterW));
            header.y = Math.round(myRoot.headerH + myRoot.unitH + myRoot.gutterH);
            var _loc_2:* = myRoot.unitW / 75;
            header.myTitle.scaleY = myRoot.unitW / 75;
            header.myTitle.scaleX = _loc_2;
            header.myTitle.x = Math.round(-3 * header.myTitle.scaleX);
            header.myTitle.y = Math.round(-6 * header.myTitle.scaleX);
            var _loc_2:* = myRoot.unitW / 75;
            header.myInput.scaleY = myRoot.unitW / 75;
            header.myInput.scaleX = _loc_2;
            header.myInput.x = 0;
            header.myInput.y = Math.round(23 * header.myInput.scaleX);
            header.inputBG.width = Math.round(411 * myRoot.unitW / 75);
            header.inputBG.height = Math.round(26 * myRoot.unitW / 75);
            header.inputBG.x = 0;
            header.inputBG.y = Math.round(22 * myRoot.unitW / 75);
            var _loc_2:* = myRoot.unitW / 75;
            header.myMessage.scaleY = myRoot.unitW / 75;
            header.myMessage.scaleX = _loc_2;
            header.myMessage.x = Math.round(-3 * header.myMessage.scaleX);
            header.myMessage.y = Math.round(50 * header.myMessage.scaleX);
            var _loc_2:* = myRoot.unitW / 75;
            header.mySubmit.scaleY = myRoot.unitW / 75;
            header.mySubmit.scaleX = _loc_2;
            header.mySubmit.x = Math.round(424 * header.mySubmit.scaleX);
            header.mySubmit.y = Math.round(23 * header.mySubmit.scaleX);
            header.myArrow.x = Math.round(header.mySubmit.x + header.mySubmit.width);
            header.myArrow.y = Math.round(header.mySubmit.y + (header.mySubmit.height - 6) / 2);
            var _loc_2:* = Math.round(61 * myRoot.unitW / 75);
            header.submitBtn.width = Math.round(61 * myRoot.unitW / 75);
            header.submitBG.width = _loc_2;
            var _loc_2:* = Math.round(26 * myRoot.unitW / 75);
            header.submitBtn.height = Math.round(26 * myRoot.unitW / 75);
            header.submitBG.height = _loc_2;
            var _loc_2:* = Math.round(420 * myRoot.unitW / 75);
            header.submitBtn.x = Math.round(420 * myRoot.unitW / 75);
            header.submitBG.x = _loc_2;
            var _loc_2:* = Math.round(22 * myRoot.unitW / 75);
            header.submitBtn.y = Math.round(22 * myRoot.unitW / 75);
            header.submitBG.y = _loc_2;
            if (nav != null)
            {
                nav.x = Math.round(-6 * myRoot.unitW / 75);
                nav.y = Math.round(header.myMessage.y + header.myMessage.height);
            }
            if (searchResults != null)
            {
                _loc_1 = 0;
                while (_loc_1 < thumbsArray.length)
                {
                    
                    thumbsArray[_loc_1].x = Math.round(_loc_1 % 3 * 4 * (myRoot.unitW + myRoot.gutterW));
                    thumbsArray[_loc_1].y = Math.round(2 * (myRoot.unitH + myRoot.gutterH) + myRoot.headerH + Math.floor(_loc_1 / 3) * 2 * (myRoot.unitH + myRoot.gutterH));
                    _loc_1++;
                }
            }
            return;
        }

        function focusOutListener(event:FocusEvent) : void
        {
            return;
        }

        function onResizer(event:Event)
        {
            doResize();
            return;
        }

        function menuClick(param1:PaginatedMenuClick)
        {
            var _loc_2:* = param1.currentTarget.currNavId;
            currNavId = param1.currentTarget.currNavId;
            startIndexNum = _loc_2;
            updateGrid();
            return;
        }

        function clickListener(event:MouseEvent) : void
        {
            var e:* = event;
            searchTerms = header.myInput.text;
            myRoot.searchTerms = searchTerms;
            header.myMessage.text = "Collecting results for \"" + searchTerms + "\"";
            if (searchResults != null)
            {
                trace("case 1");
                TweenMax.to(nav, fadeSpeed, {alpha:0, overwrite:true});
                TweenMax.to(searchResults, fadeSpeed, {alpha:0, overwrite:true, onComplete:function ()
            {
                clearResults();
                return;
            }
            });
            }
            else
            {
                trace("Case 2");
                startXML(searchTerms);
            }
            return;
        }

        function startXML(param1:String) : void
        {
            xmlLoader = new URLLoader();
            xmlLoader.addEventListener(Event.COMPLETE, initXML);
            xmlLoader.load(new URLRequest(path + param1));
            return;
        }

        function btnRollOver(event:MouseEvent)
        {
            event.currentTarget.rollOverEvent();
            return;
        }

        function btnRollOut(event:MouseEvent)
        {
            event.currentTarget.rollOutEvent();
            return;
        }

        function overListener(event:MouseEvent) : void
        {
            TweenMax.to(header.submitBG, fadeSpeed, {tint:myRoot.hotColor, alpha:1, overwrite:true});
            return;
        }

        function enterListener(event:KeyboardEvent) : void
        {
            if (event.keyCode == 13)
            {
                clickListener(null);
            }
            return;
        }

        function buildResults() : void
        {
            var _loc_1:* = undefined;
            var _loc_2:int = 0;
            var _loc_3:String = null;
            var _loc_4:String = null;
            searchResults = new Sprite();
            searchResults.alpha = 0;
            addChild(searchResults);
            _loc_1 = startIndexNum * 6;
            counter = 0;
            thumbsArray = new Array();
            _loc_2 = _loc_1;
            while (_loc_2 < _loc_1 + 6)
            {
                
                if (_loc_2 < myXMLList.length())
                {
                    if (myXMLList[_loc_2].@type == "Press Publications" || myXMLList[_loc_2].@type == "Press Releases")
                    {
                        _loc_3 = myXMLList[_loc_2].@type + "<br>" + myXMLList[_loc_2].longTitle;
                    }
                    else if (myXMLList[_loc_2].date.@season != undefined)
                    {
                        if (myXMLList[_loc_2].date.@season == "" || myXMLList[_loc_2].date.@season == " ")
                        {
                            _loc_3 = myXMLList[_loc_2].@type + "<br><span class = \'myBold\'>" + myXMLList[_loc_2].shortTitle + "</span><br>" + myXMLList[_loc_2].date.@year;
                        }
                        else
                        {
                            _loc_3 = myXMLList[_loc_2].@type + "<br><span class = \'myBold\'>" + myXMLList[_loc_2].shortTitle + "</span><br>" + myXMLList[_loc_2].date.@season + " " + myXMLList[_loc_2].date.@year;
                        }
                    }
                    else if (myXMLList[_loc_2].date.@year == "-1")
                    {
                        _loc_3 = myXMLList[_loc_2].@type + "<br><span class = \'myBold\'>" + myXMLList[_loc_2].shortTitle + "</span><br>" + "Current";
                    }
                    else if (myXMLList[_loc_2].date.@year == "-100")
                    {
                        _loc_3 = myXMLList[_loc_2].@type + "<br><span class = \'myBold\'>" + myXMLList[_loc_2].shortTitle + "</span><br>" + "Unbuilt";
                    }
                    else
                    {
                        _loc_3 = myXMLList[_loc_2].@type + "<br><span class = \'myBold\'>" + myXMLList[_loc_2].shortTitle + "</span><br>" + myXMLList[_loc_2].date.@year;
                    }
                    _loc_4 = myXMLList[_loc_2].thumbnail.@border;
                    if (_loc_4 == "")
                    {
                        _loc_4 = "0";
                    }
                    thumbnail = new RecentImage(myRoot, fadeSpeed, _loc_3, _loc_2, myXMLList[_loc_2].@id, myXMLList[_loc_2].thumbnail.@url, _loc_4);
                    searchResults.addChild(thumbnail);
                    thumbnail.buttonMode = true;
                    thumbnail.mouseChildren = false;
                    thumbnail.addEventListener(MouseEvent.MOUSE_OVER, btnRollOver);
                    thumbnail.addEventListener(MouseEvent.MOUSE_OUT, btnRollOut);
                    thumbnail.addEventListener(MouseEvent.CLICK, btnClick);
                    thumbsArray.push(thumbnail);
                }
                else
                {
                    break;
                }
                _loc_2++;
            }
            return;
        }

        function btnClick(event:MouseEvent) : void
        {
            updateAddress(myXMLList[event.currentTarget.idNum].@type, myXMLList[event.currentTarget.idNum].@id);
            return;
        }

        function updateGrid() : void
        {
            if (searchResults != null)
            {
                TweenMax.to(searchResults, fadeSpeed, {alpha:0, overwrite:true, onComplete:function ()
            {
                var _loc_1:* = undefined;
                _loc_1 = 0;
                while (_loc_1 < thumbsArray.length)
                {
                    
                    searchResults.removeChild(thumbsArray[_loc_1]);
                    thumbsArray[_loc_1] = null;
                    _loc_1 = _loc_1 + 1;
                }
                thumbsArray = [];
                removeChild(searchResults);
                searchResults = null;
                buildResults();
                doResize();
                TweenMax.to(searchResults, fadeSpeed, {alpha:1, overwrite:true});
                return;
            }
            });
            }
            return;
        }

        function clearResults() : void
        {
            var _loc_1:int = 0;
            header.removeChild(nav);
            nav.removeEventListener(PaginatedMenuClick.MENU_CLICK, menuClick);
            nav = null;
            _loc_1 = 0;
            while (_loc_1 < thumbsArray.length)
            {
                
                searchResults.removeChild(thumbsArray[_loc_1]);
                thumbsArray[_loc_1] = null;
                _loc_1++;
            }
            thumbsArray = [];
            removeChild(searchResults);
            searchResults = null;
            startXML(searchTerms);
            return;
        }

        function updateAddress(param1:String, param2:int) : void
        {
            var incomingType:* = param1;
            var incomingId:* = param2;
            trace(incomingType + " " + incomingId);
            switch(incomingType)
            {
                case "Awards":
                {
                    myRoot.clickPathNames = [""];
                    myRoot.updateAddress("/" + "practice" + "/" + "awards" + "/" + 0 + "/" + incomingId + "/" + 0 + "/");
                    break;
                }
                case "Partners":
                {
                    myRoot.clickPathNames = [""];
                    myRoot.updateAddress("/" + "practice" + "/" + "partners" + "/" + 0 + "/" + incomingId + "/" + 0 + "/");
                    break;
                }
                case "New York Staff":
                {
                    myRoot.clickPathNames = [""];
                    myRoot.updateAddress("/" + "practice" + "/" + "staff" + "/" + "new-york" + "/" + "senior-staff" + "/" + 0 + "/" + incomingId + "/" + 0 + "/");
                    break;
                }
                case "Los Angeles Staff":
                {
                    myRoot.clickPathNames = [""];
                    myRoot.updateAddress("/" + "practice" + "/" + "staff" + "/" + "los-angeles" + "/" + "senior-staff" + "/" + 0 + "/" + incomingId + "/" + 0 + "/");
                    break;
                }
                case "Press Publications":
                {
                    myRoot.clickPathNames = [""];
                    myRoot.updateAddress("/" + "practice" + "/" + "press" + "/" + "publications" + "/" + "archive" + "/" + 0 + "/" + incomingId + "/" + 0 + "/");
                    break;
                }
                case "Press Releases":
                {
                    myRoot.clickPathNames = [""];
                    myRoot.updateAddress("/" + "practice" + "/" + "press" + "/" + "releases" + "/" + "archive" + "/" + 0 + "/" + incomingId + "/" + 0 + "/");
                    break;
                }
                case "Lectures":
                {
                    myRoot.clickPathNames = [""];
                    myRoot.updateAddress("/" + "practice" + "/" + "lectures" + "/" + 0 + "/" + incomingId + "/" + 0 + "/");
                    break;
                }
                case "Books":
                {
                    myRoot.clickPathNames = [""];
                    myRoot.updateAddress("/" + "practice" + "/" + "books" + "/" + "archive" + "/" + 0 + "/" + incomingId + "/" + 0 + "/");
                    break;
                }
                case "Selected Text":
                {
                    myRoot.clickPathNames = [""];
                    myRoot.updateAddress("/" + "practice" + "/" + "selected-text" + "/" + 0 + "/" + incomingId + "/" + 0 + "/");
                    break;
                }
                case "Press Video":
                {
                    myRoot.updateAddress("/" + "practice" + "/" + "press" + "/" + "video" + "/" + 0 + "/" + incomingId + "/" + 0 + "/");
                    break;
                }
                case "Architecture":
                {
                    var _loc_5:int = 0;
                    var _loc_8:int = 0;
                    var _loc_11:int = 0;
                    var _loc_12:* = myRoot.myXML.nav.projects.tier0;
                    var _loc_10:* = new XMLList("");
                    for each (_loc_13 in _loc_12)
                    {
                        
                        var _loc_14:* = _loc_12[_loc_11];
                        with (_loc_12[_loc_11])
                        {
                            if (@label == "Architecture")
                            {
                                _loc_10[_loc_11] = _loc_13;
                            }
                        }
                    }
                    var _loc_9:* = _loc_10.tier1;
                    var _loc_7:* = new XMLList("");
                    for each (_loc_10 in _loc_9)
                    {
                        
                        var _loc_11:* = _loc_9[_loc_8];
                        with (_loc_9[_loc_8])
                        {
                            if (@label == "Name")
                            {
                                _loc_7[_loc_8] = _loc_10;
                            }
                        }
                    }
                    var _loc_6:* = _loc_7.tier2.item;
                    var _loc_4:* = new XMLList("");
                    for each (_loc_7 in _loc_6)
                    {
                        
                        var _loc_8:* = _loc_6[_loc_5];
                        with (_loc_6[_loc_5])
                        {
                            if (@id == incomingId)
                            {
                                _loc_4[_loc_5] = _loc_7;
                            }
                        }
                    }
                    if (_loc_4.@sub == "T")
                    {
                        myRoot.clickPathNames = [""];
                        myRoot.updateAddress("/" + "projects" + "/" + "architecture" + "/" + "name" + "/" + 0 + "/" + incomingId + "/" + "sub" + "/" + 0 + "/" + 0 + "/");
                    }
                    else
                    {
                        myRoot.clickPathNames = [""];
                        myRoot.updateAddress("/" + "projects" + "/" + "architecture" + "/" + "name" + "/" + 0 + "/" + incomingId + "/" + 0 + "/");
                    }
                    break;
                }
                case "Products":
                {
                    var _loc_5:int = 0;
                    var _loc_8:int = 0;
                    var _loc_11:int = 0;
                    var _loc_12:* = myRoot.myXML.nav.projects.tier0;
                    var _loc_10:* = new XMLList("");
                    for each (_loc_13 in _loc_12)
                    {
                        
                        var _loc_14:* = _loc_12[_loc_11];
                        with (_loc_12[_loc_11])
                        {
                            if (@label == "Products")
                            {
                                _loc_10[_loc_11] = _loc_13;
                            }
                        }
                    }
                    var _loc_9:* = _loc_10.tier1;
                    var _loc_7:* = new XMLList("");
                    for each (_loc_10 in _loc_9)
                    {
                        
                        var _loc_11:* = _loc_9[_loc_8];
                        with (_loc_9[_loc_8])
                        {
                            if (@label == "Name")
                            {
                                _loc_7[_loc_8] = _loc_10;
                            }
                        }
                    }
                    var _loc_6:* = _loc_7.tier2.item;
                    var _loc_4:* = new XMLList("");
                    for each (_loc_7 in _loc_6)
                    {
                        
                        var _loc_8:* = _loc_6[_loc_5];
                        with (_loc_6[_loc_5])
                        {
                            if (@id == incomingId)
                            {
                                _loc_4[_loc_5] = _loc_7;
                            }
                        }
                    }
                    if (_loc_4.@sub == "T")
                    {
                        myRoot.clickPathNames = [""];
                        myRoot.updateAddress("/" + "projects" + "/" + "products" + "/" + "name" + "/" + 0 + "/" + incomingId + "/" + "sub" + "/" + 0 + "/" + 0 + "/");
                    }
                    else
                    {
                        myRoot.clickPathNames = [""];
                        myRoot.updateAddress("/" + "projects" + "/" + "products" + "/" + "name" + "/" + 0 + "/" + incomingId + "/" + 0 + "/");
                    }
                    break;
                }
                case "Exhibitions":
                {
                    var _loc_5:int = 0;
                    var _loc_8:int = 0;
                    var _loc_11:int = 0;
                    var _loc_12:* = myRoot.myXML.nav.projects.tier0;
                    var _loc_10:* = new XMLList("");
                    for each (_loc_13 in _loc_12)
                    {
                        
                        var _loc_14:* = _loc_12[_loc_11];
                        with (_loc_12[_loc_11])
                        {
                            if (@label == "Exhibitions")
                            {
                                _loc_10[_loc_11] = _loc_13;
                            }
                        }
                    }
                    var _loc_9:* = _loc_10.tier1;
                    var _loc_7:* = new XMLList("");
                    for each (_loc_10 in _loc_9)
                    {
                        
                        var _loc_11:* = _loc_9[_loc_8];
                        with (_loc_9[_loc_8])
                        {
                            if (@label == "Name")
                            {
                                _loc_7[_loc_8] = _loc_10;
                            }
                        }
                    }
                    var _loc_6:* = _loc_7.tier2.item;
                    var _loc_4:* = new XMLList("");
                    for each (_loc_7 in _loc_6)
                    {
                        
                        var _loc_8:* = _loc_6[_loc_5];
                        with (_loc_6[_loc_5])
                        {
                            if (@id == incomingId)
                            {
                                _loc_4[_loc_5] = _loc_7;
                            }
                        }
                    }
                    if (_loc_4.@sub == "T")
                    {
                        myRoot.clickPathNames = [""];
                        myRoot.updateAddress("/" + "projects" + "/" + "exhibitions" + "/" + "name" + "/" + 0 + "/" + incomingId + "/" + "sub" + "/" + 0 + "/" + 0 + "/");
                    }
                    else
                    {
                        myRoot.clickPathNames = [""];
                        myRoot.updateAddress("/" + "projects" + "/" + "exhibitions" + "/" + "name" + "/" + 0 + "/" + incomingId + "/" + 0 + "/");
                    }
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }

        function initXML(event:Event) : void
        {
            var _loc_2:* = undefined;
            myXML = new XML(xmlLoader.data);
            trace("myXML " + myXML);
            myXMLList = new XMLList(myXML.item);
            header.myMessage.text = myXMLList.length() + " results for \"" + searchTerms + "\"";
            if (myXMLList.length() > 0)
            {
                startIndexNum = 0;
                _loc_2 = Math.ceil(myXML.item.length() / 6);
                nav = new ProjPaginatedMenu(myRoot, fadeSpeed, _loc_2);
                nav.addEventListener(PaginatedMenuClick.MENU_CLICK, menuClick, false, 0, true);
                nav.alpha = 0;
                header.addChild(nav);
                buildResults();
                doResize();
                TweenMax.to(nav, fadeSpeed, {alpha:1, overwrite:true});
                TweenMax.to(searchResults, fadeSpeed, {alpha:1, overwrite:true});
            }
            return;
        }

        function focusInListener(event:FocusEvent) : void
        {
            if (stage.displayState == StageDisplayState.FULL_SCREEN)
            {
                myRoot.closeFullscreen();
            }
            return;
        }

    }
}
